/////////////////////////////////////////////////////////////////////////////
//
// (c) 2007 BinaryComponents Ltd.  All Rights Reserved.
//
// http://www.binarycomponents.com/
//
/////////////////////////////////////////////////////////////////////////////

using System.Drawing;
using SuperListEx.Sections;
using SuperListEx.Utility.Collections;

namespace SuperListEx.Helper
{
  internal class AvailableSectionsControl : SectionContainerControl
  {
    #region vars
    private EventingList<Column> _availableColumns = new EventingList<Column>();
    private ColumnList _columnList;
    #endregion

    #region properties
    public ColumnList ColumnList
    {
      get
      {
        return _columnList;
      }
      set
      {
        if (_columnList != value)
        {
          if (_columnList != null)
          {
            _columnList.DataChanged -= columnList_DataChanged;
            _availableColumns.Clear();
          }

          _columnList = value;

          if (_columnList != null)
          {
            foreach (Column column in _columnList)
            {
              _availableColumns.Add(column);
            }
            _columnList.DataChanged += columnList_DataChanged;
          }
        }
      }
    }
    #endregion

    #region constructors
    public AvailableSectionsControl()
    {
      InitializeComponent();
    }
    #endregion

    #region public functions
    public override void LayoutControl()
    {
      StopLazyUpdateTimer();
      Canvas.Location = Point.Empty;
      using (Graphics grfx = CreateGraphics())
      {
        Canvas.Layout(new GraphicsSettings(grfx), new Size(ClientRectangle.Width, 1024));
      }
      Invalidate();

      Height = Canvas.Rectangle.Height;
    }
    #endregion

    #region protected functions
    protected override void Dispose(bool disposing)
    {
      base.Dispose(disposing);

      if (_columnList != null)
      {
        _columnList.DataChanged -= columnList_DataChanged;
        _columnList = null;
      }
    }

    protected override SectionContainer CreateCanvas()
    {
      return new AvailableColumnsSection(this, _availableColumns);
    }
    #endregion

    #region private functions
    private void columnList_DataChanged(object sender, EventInfo<Column> e)
    {
      switch (e.EventType)
      {
        case EventType.Deleted:
          foreach (Column c in e.Items)
          {
            _availableColumns.Remove(c);
          }
          break;
        case EventType.Added:
          foreach (Column c in e.Items)
          {
            _availableColumns.Add(c);
          }
          break;
        default:
          break;
      }
    }

    private void InitializeComponent()
    {
      SuspendLayout();
      ResumeLayout(false);
    }
    #endregion
  }
}